<?php

namespace app\adminapi\controller;

use think\Cache;
use think\cache\driver\Redis;
use think\Controller;
use think\Request;
use tools\jwt\Token;

class Messages extends Common
{

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //接收数据
        $params = input();
        //验证数据
        $validate = $this->validate($params,[
           'comment|留言内容' => 'require|max:200|chsDash',
        ]);
        if($validate !== true){
            $this->fail($validate);
        }
        //获取自增id
        $id=$this->redis->incr('id');
        //往redis里添加数据
        $res=$this->redis->hmSet('messages_'.$id,array('id'=>$id,'username'=>$params['username'],'comment'=>$params['comment'],'time'=>time()));
        //返回数据
        if($res==true){
            $data = $this->redis->hGetAll('messages_'.$id);
            $this->ok($data);
        }else{
            $this->fail('添加留言出错！');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //查询数据
        $data = $this->redis->hGetAll('messages_'.$id);
        $this->ok($data);
    }
}
